package me.caofeng.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.stereotype.Component;

import javax.print.DocFlavor;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.util
 * @Description: json 序列化  时间格式转换器
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-23 8:23
 * @Version: V1.0
 */
@Component
public class JsonDateSerializer extends JsonSerializer<Date>{
    private static final SimpleDateFormat dateFormat =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        String formatDate = dateFormat.format(value);
        gen.writeString(formatDate);
    }
}
